package com.ejoysoft.wlanyz.business; 

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.ejoysoft.common.DbConnect;
import com.ejoysoft.common.Format;
import com.ejoysoft.common.Globa;
import com.ejoysoft.common.UID;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import com.ejoysoft.common.*;
public class Readxml
{
	private Globa globa =new Globa();
    private DbConnect dd = new DbConnect();
    
	public Readxml(){} 
	public Readxml(Globa globa) {
        this.globa = globa;
        dd = globa.db;
    }

    public Readxml(Globa globa, boolean b) {
        this.globa = globa;
        dd = globa.db;
        if (b) globa.setDynamicProperty(this);
    }
	private String floor = "";
	private String paperid = "";
    private String CADname = "";
    private String transmitter = "";
    private String num = "";
    private double rsn = 0 ;
    private double xaxes = 0;
    private double yaxes = 0;
    private File f = new File("");
	String strTableName = "ac_cad_info";
    public boolean read(String creator,String AcName,String XML)throws SQLException
    {//String ID,String CAD,String ACName,String classtwoac
    	InputStream inputStream = null;
    	String code = ""; 
    	try
        {
    		//获取文件的保存编码格式
    		inputStream = new FileInputStream(XML);
    		byte[] head = new byte[3];
    		inputStream.read(head);
    		code = "GB3212";
    		if(head[0]==-1&&head[1]==-2)
    			code = "UTF-16";
    		if(head[0]==-2&&head[1]==-1)
    			code = "Unicode";
    		if(head[0]==-17&&head[1]==-69&&head[2]==-65)
    			code = "UTF-8";
    		System.out.println("0000000000"+code);
    		if(!code.equals("UTF-8"))
    		{
    			System.out.println("0000000000"+code);
    			/*File file = new File(XML);
    			Writer outTxt = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");

    	        outTxt.write("文件内容格式不正确, 此文件已被系统删除! ");

    	        outTxt.close();*/


    			StringBuilder sBuilder = new StringBuilder("");
    			
    			BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(XML),"UTF-8"));
    			String line = null; 
                while ((line = bReader.readLine()) != null)  
               {
                	 sBuilder.append(line+"\r\n"); 

    		  }
               bReader.close();
               File file = new File(XML);
               if(file.isDirectory())file.delete();
               File dir = new File(XML);
               if (!dir.exists()) { 
                   dir.mkdirs(); 
               } 
               FileOutputStream fos = new FileOutputStream(XML); 
               Writer out = new OutputStreamWriter(fos, "UTF-8"); 
               out.write(sBuilder.toString()); 
               out.close(); 
               fos.close(); 
         	}
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            f = new File(XML);
            Document doc = db.parse(f);//输入流对象
            Element elmtInfo = doc.getDocumentElement();
            NodeList nodes = elmtInfo.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++)
            { 
                Node result = nodes.item(i);
                if (result.getNodeType() == Node.ELEMENT_NODE && result.getNodeName().equals("floor"))
                {
//                	System.out.print(i*100);
                    NodeList ns = result.getChildNodes();

                    for (int j = 0; j < ns.getLength(); j++)
                    {
                        Node record = ns.item(j);

                        if (record.getNodeType() == Node.ELEMENT_NODE && record.getNodeName().equals("name"))
                        {
                        	 floor = record.getTextContent();
                            System.out.println( "楼层: " + record.getTextContent());
                           
                        }
                        if (record.getNodeType() == Node.ELEMENT_NODE && record.getNodeName().equals("image"))
                        {
                        	paperid=UID.getID();
                        	CADname = record.getTextContent();
                            System.out.println( "图纸名称: " + record.getTextContent());
                           
                        }
                        if (record.getNodeType() == Node.ELEMENT_NODE && record.getNodeName().equals("transmitters"))
                        {
                        	
                        	NodeList nl = record.getChildNodes();
                             for(int k = 0;k < nl.getLength(); k++)
                             {
                            	 Node recod1 = nl.item(k);
                            	 if (recod1.getNodeType() == Node.ELEMENT_NODE &&recod1.getNodeName().equals("transmitter"))
                            	 {
                            		 NodeList ng = recod1.getChildNodes();
                            		 for(int g = 0;g < ng.getLength(); g++)
                            		 {
                            			 Node recod2 = ng.item(g);
                            			 if (recod2.getNodeType() == Node.ELEMENT_NODE &&recod2.getNodeName().equals("name"))
                            			 {
                            				 transmitter = recod2.getTextContent();
                            				System.out.println( "发射源: " + recod2.getTextContent());
                                           
                            			 }
                            			 if (recod2.getNodeType() == Node.ELEMENT_NODE &&recod2.getNodeName().equals("num"))
                            			 {
                            				 num = recod2.getTextContent();
                            				 System.out.println( "编号: " + recod2.getTextContent());
                                             
                            			 }
                            			 if (recod2.getNodeType() == Node.ELEMENT_NODE &&recod2.getNodeName().equals("out"))
                            			 {
                            				 rsn = Double.parseDouble(recod2.getTextContent());
                            				 System.out.println( "场强: " + recod2.getTextContent());
                                          
                            			 }
                            			 if (recod2.getNodeType() == Node.ELEMENT_NODE &&recod2.getNodeName().equals("x"))
                            			 {
                            				 xaxes = Double.parseDouble(recod2.getTextContent());
                            				 System.out.println("横坐标: " + recod2.getTextContent());
                                           
                            			 }
                            			 if (recod2.getNodeType() == Node.ELEMENT_NODE &&recod2.getNodeName().equals("y"))
                            			 {
                            				 yaxes = Double.parseDouble(recod2.getTextContent());
                            				 System.out.println( "纵坐标: " + recod2.getTextContent());
                            			 }
                            		 }
                            		 String strSql="";
                            		 try {
              			        	//把用户信息写入数据库
                            		String strTempId = UID.getID();
              			            strSql = "INSERT INTO " + strTableName + "  (strId, paperid,acname, cadPic , intfloor, STRCREATOR, dtcreattime ,RADIATE,RADNUM,RSN,XAXES,YAXES,imgext) VALUES (?,?,?,?,?,?,to_date(?,'yyyy-MM-dd hh24:mi:ss'),?,?,?,?,?,?)";          
              			            dd.prepareStatement(strSql);
              			            dd.setString(1, strTempId);
              			            dd.setString(2, paperid);
              			            dd.setString(3, AcName);
              			            dd.setString(4, CADname);
              			            dd.setString(5, floor);
              			            dd.setString(6, creator);
              			            dd.setString(7, Format.getDateTime());
              			            dd.setString(8, transmitter);
              			            dd.setString(9, num);
              			            dd.setDouble(10, rsn);
              			            dd.setDouble(11, xaxes);
              			            dd.setDouble(12, yaxes);
              			            dd.setInt(13, 0);
              			            if (dd.executeUpdate() > 0) {
              			            	
              			                Globa.logger0("增加图纸信息", globa.loginName, globa.loginIp, strSql, "图纸管理", globa.userSession.getStrDepart());
              			            //  System.out.println(globa.userSession.getStrDepart()+globa.loginName+globa.loginIp+floor+CADname+transmitter+num+rsn+xaxes+yaxes+"rrrrrrrrrrrr");
                                      System.out.println("*************************************************增加图纸数据成功");
                             	     } else{    
                             	    	 System.out.println("*************************************************增加图纸数据失败");
                                  	
                             	     }
              			           
              			        } catch (Exception e) {
              			        	dd.rollback();
              			            System.out.println("增加图纸信息时出错！！"+e);
              			            e.printStackTrace();
              			          f.delete();
              			          return false;
              			        } 
                            		 
                            		
              			        
                            	 }
                             }
                     
                        }
                      
                    }
                
                }
            }
           return true;
        }
        catch (ParserConfigurationException e)
        {
        	dd.rollback();
        	f.delete();
            e.printStackTrace();
            return false;
            
        }
        catch (SAXException e)
        {
        	dd.rollback();
        	f.delete();
            e.printStackTrace();
            return false;
        }
        catch (IOException e)
        {
        	dd.rollback();
        	f.delete();
            e.printStackTrace();
            return false;
        }
    }
}